package multimedia;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import ui.VadeMecumCameraCanvas;
import ui.VadeMecumImageViewer;

public class SnapshotThread implements Runnable, CommandListener {

	private VadeMecumCameraCanvas canvas;
	
	private VadeMecumImageViewer viewer;
	
	private boolean snapshotTaken = false;
	
	public SnapshotThread(VadeMecumCameraCanvas canvas) {
		this.snapshotTaken = false;
		this.canvas = canvas;
	}
	
	public boolean getTakenPicture() {
		return this.snapshotTaken;
	}
	
	public void run() {
		try {
			//use the control to take the picture, using the defatult encoding
			byte[] imageArray = canvas.videoControl.getSnapshot(null);
			
			viewer = new VadeMecumImageViewer(canvas.midlet, imageArray);			
			viewer.setCommandListener(this);
			
			canvas.midlet.display.setCurrent(viewer);
		} catch (Exception e) {
			canvas.midlet.error(e);
		}
	}

	public void commandAction(Command cmd, Displayable disp) {
		if (cmd == viewer.back) {
			canvas.midlet.display.setCurrent(canvas);
		} else if (cmd == viewer.save) {
			viewer.saveSnapshot();
		} else if (cmd == viewer.sendToServiceWhithoutContext) {
			viewer.sendWithoutContext();
		} else if (cmd == viewer.sendToServiceWithContext) {
			viewer.sendWithContext();
		}		
	}

}
